﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Sdari.Core.Exceptions;
using Sdari.Example.Business.Database;
using Sdari.Example.Models;

namespace Sdari.Pmp.Models
{
    public static class ExampleBusinessExtentions
    {
        /// <summary>
        /// 初始化数据库链接
        /// </summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static IApplicationBuilder UsePmpBusiness(this IApplicationBuilder app) =>
            app.Use(async (c, next) =>
            {
                Db.Init(c.RequestServices.GetService<IExampleDbContext>() ?? throw new ServiceLoadFailedException<IExampleDbContext>());
                await next();
            });
    }
}
